﻿using System;
using System.Configuration;
using System.Messaging;
using System.ServiceModel;
using VideoServices.PublishService;
using VideoServices.Services;

namespace TestDriver.Host
{
    class Program
    {
        private const string MSG = "{0} Service started!";

        static void Main(string[] args)
        {
            CreateQueue();

            Console.WriteLine("Starting services...");
            try
            {
                // Create a ServiceHost for the OrderStatus service type.
                var deliveryHost = new ServiceHost(typeof(DeliveryService));
                deliveryHost.Open();
                Console.WriteLine(MSG, "Delivery");

                var deliveryStatusHost = new ServiceHost(typeof(DeliveryStatusService));
                deliveryStatusHost.Open();
                Console.WriteLine(MSG, "Delivery Status");

                var customerHost = new ServiceHost(typeof(CustomerService));
                customerHost.Open();
                Console.WriteLine(MSG, "Customer");

                var catalogueHost = new ServiceHost(typeof(CatalogueService));
                catalogueHost.Open();
                Console.WriteLine(MSG, "Catalogue");

                var orderHost = new ServiceHost(typeof(OrderService));
                orderHost.Open();
                Console.WriteLine(MSG, "Order");

                var stockHost = new ServiceHost(typeof(StockService));
                stockHost.Open();
                Console.WriteLine(MSG, "Stock");

                var storeHost = new ServiceHost(typeof(StoreService));
                storeHost.Open();
                Console.WriteLine(MSG, "Store");

                var warehouseHost = new ServiceHost(typeof(WarehouseService));
                warehouseHost.Open();
                Console.WriteLine(MSG, "Warehouse");

                // Open the ServiceHostBase to create listeners and start listening for delivery status messages.
                Console.WriteLine();
                Console.WriteLine("All Services started successfully!");
                Console.WriteLine();
                Console.WriteLine("Now listening to any Delivery Status update ... ");
                Console.WriteLine();
                Console.ReadKey();
            }
            catch (CommunicationException cmEx)
            {
                Console.WriteLine("Could not start services. Error: " + cmEx.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Could not start services. Error: " + ex.Message);
            }
        }

        private static void CreateQueue()
        {
            string queueName = ConfigurationManager.AppSettings["queueName"];
            if (!MessageQueue.Exists(queueName))
            {
                MessageQueue.Create(queueName, true);
            }

            queueName = ConfigurationManager.AppSettings["statusQueueName"];
            if (!MessageQueue.Exists(queueName))
            {
                MessageQueue.Create(queueName, true);
            }
        }
    }
}